#include <bits/stdc++.h>
// 2025/02/04
// tag: 
// Author: Zhang Muen
using namespace std;

using int64 = int64_t;

int n, k, nums[1000001], ans[1000001][2];

signed main()
{
    ios::sync_with_stdio(false);
    cin.tie(nullptr), cin.tie(nullptr);
    cin >> n >> k;
    for (int i = 1; i <= n; ++i)
        cin >> nums[i];
    multiset<int> s;
    for (int i = 1; i <= k; i++)
        s.insert(nums[i]);
    ans[1][0] = *s.begin(), ans[1][1] = *s.rbegin();
    for (int i = 2; i <= n - k + 1; i++)
        s.erase(s.find(nums[i - 1])), s.insert(nums[i + k - 1]), ans[i][0] = *s.begin(), ans[i][1] = *s.rbegin();
    for (int i = 1; i <= n - k + 1; i++)
        cout << ans[i][0] << ' ';
    cout << endl;
    for (int i = 1; i <= n - k + 1; i++)
        cout << ans[i][1] << ' ';
    return 0;
}